package com.jiuwan.gamepass.service.userImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.jiuwan.gamepass.dao.authDao.SysApiDao;
import com.jiuwan.gamepass.dto.authDto.Result;
import com.jiuwan.gamepass.dto.authDto.ResultCode;
import com.jiuwan.gamepass.pojo.authPojo.SysApi;
import com.jiuwan.gamepass.service.SysApiService;
import com.sun.org.apache.xerces.internal.util.SynchronizedSymbolTable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * @version 1.0
 * 功能目的描述 ：用于         ========>>>>>>>>>>>
 * @author： pang-yun
 * @date： 2021-06-29 10:00
 */
@Service
public class SysApiServiceImpl implements SysApiService {

    @Autowired
    private SysApiDao sysApiDao;

    @Override
    public Result selectById(int id) {
        if (String.valueOf(id).equals("")) {
            Result error = Result.error(ResultCode.PARAM_NOT_BLANK);
            return error;
        }
        List<SysApi> sysApis = sysApiDao.selectAllById(id);
        Result ok = Result.ok();

        HashMap<String, Object> map = new HashMap<>();
        map.put("apis", sysApis);
        ok.setData(map);
        return ok;
    }

    @Override
    public Result addApi(SysApi sysApi) {
        if (sysApi == null) {
            Result error = Result.error(ResultCode.PARAM_NOT_BLANK);
            return error;
        }
        int insert = sysApiDao.insert(sysApi);
        if (insert == 0) {
            Result error = Result.error();
            return error;
        }
        Result ok = Result.ok();
        return ok;
    }

    @Override
    public Result deleteByIdAndUrl(String id, String url) {
        if (id == null || url == null) {
            Result error = Result.error(ResultCode.PARAM_NOT_BLANK);
            return error;
        }
        int i = sysApiDao.deleteByIdAndUrl(id, url);
        if (i == 0) {
            Result error = Result.error();
            return error;
        }
        Result ok = Result.ok();
        return ok;
    }

    @Override
    public Result selectPage(int  id,int current,int size) {
        Page<SysApi> page = new Page<>(current, size);
        QueryWrapper<SysApi> wrapper = new QueryWrapper<>();
        Page<SysApi> page1 = sysApiDao.selectPage(page, wrapper.eq("id", id));

        Result ok = Result.ok();
        HashMap<String, Object> map = new HashMap<>();
        map.put("page", page1);

        ok.setData(map);
        return ok;
    }


}
